package com.example.diary;

import static com.example.diary.DatabaseHelper.DB_NAME;
import static com.example.diary.DatabaseHelper.TABLE_NAME;
import static com.example.diary.DatabaseHelper.TABLE_NAME_USER;
import static com.example.diary.DatabaseHelper.VERSION;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * @author zengzl
 * 用户注册界面
 */

public class RegActivity extends AppCompatActivity implements View.OnClickListener {
    public static DatabaseHelper dbHelper;
    private EditText USERNAME;
    private EditText PASSWORD;
    private EditText P_NUMBER;
    private Button BTN_REGISTER;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隐藏状态栏 调试通过，写入ppt
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.register_activity);
        init();
    }

    private void init() {
        USERNAME = findViewById(R.id.R_username);
        PASSWORD = findViewById(R.id.R_password);
        P_NUMBER = findViewById(R.id.R_pnumber);
        BTN_REGISTER = findViewById(R.id.btn_register);
        BTN_REGISTER.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        AlertDialog dialog;
        String name, password, phone;
        SQLiteDatabase db;
        ContentValues values;
        switch (v.getId()) {
            case R.id.btn_register:
                name = USERNAME.getText().toString();
                password = PASSWORD.getText().toString();
                phone = P_NUMBER.getText().toString();
                if (CheckIsDataAlreadyInDBorNot(name)) {
//                    Toast.makeText(this, "该用户名已被注册，注册失败", Toast.LENGTH_SHORT).show();
                    AlertDialog.Builder builder = new AlertDialog.Builder(this)
                            .setTitle("警告")
                            //yh，替换图标done
                            .setIcon(R.mipmap.warn)
                            .setMessage("该用户名已被注册，注册失败")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    dialog = builder.create();
                    dialog.show();
                    // debug 2021-11-21 dialog弹窗按钮字体颜色重现
                    Button btnPos = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
                    Button btnNeg = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
                    btnPos.setTextColor(Color.RED);
                    btnNeg.setTextColor(Color.GRAY);
                }else if(name.length() == 0){
                    AlertDialog.Builder builder = new AlertDialog.Builder(this)
                            .setTitle("警告")
                            //yh，替换图标done
                            .setIcon(R.mipmap.warn)
                            .setMessage("用户名为空，请输入后再保存")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    dialog = builder.create();
                    dialog.show();
                    // debug 2021-11-21 dialog弹窗按钮字体颜色重现
                    Button btnPos = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
                    Button btnNeg = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
                    btnPos.setTextColor(Color.RED);
                    btnNeg.setTextColor(Color.GRAY);
                }else if(password.length() == 0){
                    AlertDialog.Builder builder = new AlertDialog.Builder(this)
                            .setTitle("警告")
                            //yh，替换图标done
                            .setIcon(R.mipmap.warn)
                            .setMessage("密码为空，请输入后再保存")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    dialog = builder.create();
                    dialog.show();
                    // debug 2021-11-21 dialog弹窗按钮字体颜色重现
                    Button btnPos = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
                    Button btnNeg = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
                    btnPos.setTextColor(Color.RED);
                    btnNeg.setTextColor(Color.GRAY);
                }else if(phone.length() == 0){
                    AlertDialog.Builder builder = new AlertDialog.Builder(this)
                            .setTitle("警告")
                            //yh，替换图标done
                            .setIcon(R.mipmap.warn)
                            .setMessage("手机号为空，请输入后再保存")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    dialog = builder.create();
                    dialog.show();
                    // debug 2021-11-21 dialog弹窗按钮字体颜色重现
                    Button btnPos = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
                    Button btnNeg = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
                    btnPos.setTextColor(Color.RED);
                    btnNeg.setTextColor(Color.GRAY);
                } else{
                    dbHelper = new DatabaseHelper(RegActivity.this, DB_NAME, null, VERSION);
                    db = dbHelper.getWritableDatabase();   //获取可读写SQLiteDatabase
                    values = new ContentValues();
                    values.put("name", name);
                    values.put("password", password);
                    values.put("phone", phone);

                    db.insert(TABLE_NAME_USER, null, values);
                    values.clear();
                    // 调用Intent返回MainActivity页面，达成刷新的效果
                    Intent intent = new Intent(RegActivity.this, Diary.class);
                    startActivity(intent);
                    //结束当前页面活动
                    finish();
                    Toast.makeText(RegActivity.this, "已注册成功", Toast.LENGTH_SHORT).show();
                    db.close();
                }
                break;
        }

    }


    //检验用户名是否已存在
    public boolean CheckIsDataAlreadyInDBorNot(String value) {
        dbHelper = new DatabaseHelper(RegActivity.this, DB_NAME, null, VERSION);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String Query = "Select * from User where name = ?";
        Cursor cursor = db.rawQuery(Query, new String[]{value});
        if (cursor.getCount() > 0) {
            cursor.close();
            return true;
        }
        cursor.close();
        return false;
    }
}